package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationCountVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * @className: ReservationServiceImpl
 * @description:
 * @author: InnovatorX
 * @date: 2024/5/30 11:52
 * @Version: 1.0
 */
@Service
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public PageResponse<ReservationVo> findAllReservationsByPages(Integer pageNum, Integer pageSize, String status) {
        PageHelper.startPage(pageNum, pageSize);
        List<ReservationVo> list = reservationMapper.findReservationsByPage(status);
        Page<ReservationVo> page =(Page<ReservationVo>)list;
        return PageResponse.of(page, ReservationVo.class);
    }

    @Override
    public ReservationVo findReservationById(Long id) {
        return reservationMapper.findReservationById(id);
    }

    @Override
    public void addReservation(ReservationDto reservation) {
        reservation.setStatus(0);
        reservationMapper.insertReservation(reservation);
    }

    @Override
    public void updateReservation(ReservationDto reservation) {
        reservationMapper.updateReservation(reservation);
    }

    @Override
    public void cancelReservationById(Long id) {
        reservationMapper.updateReservationById(id);
    }

    @Override
    public Long getCancelledCount() {
        // 获取小程序的的用户ID
        Long userId = UserThreadLocal.getUserId();
        // 生成当天开始和结束的时间
        LocalDateTime begin = LocalDate.now().atStartOfDay();
        LocalDateTime end = LocalDate.now().atTime(23, 59, 59);
        // 根据ID查询当天的取消预约数量
        Long cancelledCount = reservationMapper.getCancelledCount(userId, begin, end);
        return cancelledCount;
    }

    /**
     * 每个时段预约次数为6次
     * @param time 毫秒值
     * @return
     */
    @Override
    public List<ReservationCountVo> countByTime(Long time) {
//        把毫秒值转换为LocalDateTime
        LocalDateTime start = Instant.ofEpochMilli(time).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime().plusHours(8);
        LocalDateTime end = start.plusHours(18);
        // 查询数据库中当天时段的预约次数
        List<ReservationCountVo> list = reservationMapper.countByTime(start,end);
        List<ReservationCountVo> result = reservationMapper.countByTime(start,end);
        // 把预约时间段补全
        // 每天从八点开始
        while (start.isBefore(end)|| start.isEqual(end)){
            ReservationCountVo countVo = new ReservationCountVo(6, start);
            for (ReservationCountVo vo : list) {
                if (vo.getTime().equals(start)){
                    countVo.setCount(6-vo.getCount());
                    break;
                }
            }
            result.add(countVo);
        }
        return list;
    }

    @Override
    public void overdueReservation() {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        reservationMapper.overdueReservation(now);
    }
}

